package com.zzzyh.forumsystemclient.services.impl;


import com.zzzyh.forumsystemclient.dao.UserMapper;
import com.zzzyh.forumsystemclient.model.LoginUser;
import com.zzzyh.forumsystemclient.model.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.InternalAuthenticationServiceException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
@Slf4j
public class MyUserDetailsByMysql implements UserDetailsService {
    @Autowired
    private UserMapper userMapper;

//    @Autowired
//    private ResourceMapper resourceMapper;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = userMapper.selectByUsername(username);
        log.info("用户信息为：{}", user);
        if(user == null){
            throw new InternalAuthenticationServiceException("用户不存在");
        }
//        List<String> permissions = resourceMapper.findPermissionsByUserId(user.getId());
        List<String> permissions = null;
        log.info("用户权限为：{}", permissions);
        return new LoginUser(user, permissions);
    }
}
